//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"SelectSquadState.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "SelectSquadState.h"
#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "../../SGD Wrappers/CSGD_MessageSystem.h"
#include "../../SGD Wrappers/CSGD_EventSystem.h"
#include "../../SGD Wrappers/SGD_String.h"
#include "../../SGD Wrappers/SGD_Math.h"
#include "../MapSystem/BattleMap.h"
#include "../ParticleSystem/Emitter.h"
#include "../Game.h"
#include "../GameObjects/General.h"
#include "../GameObjects/Knight.h"
#include "../GameObjects/Ranger.h"
#include "../GameObjects/Priest.h"
#include "../GameObjects/Berserker.h"
#include <cassert>
#include "../GameObjects/Player.h"
#include "GameplayState.h"
#include <fstream>
#include "GameLoadState.h"

SelectSquadState* SelectSquadState::GetInstance()
{
	static SelectSquadState Instance;

	return &Instance;
}
SelectSquadState::SelectSquadState()
{
	cursor = 0;
}
SelectSquadState::~SelectSquadState()
{
}
/*virtual*/ void SelectSquadState::Enter()
{
	cursor = 0;
	titleImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MapStage3.png"));
	playerOne = GameLoadState::GetInstance()->playerJuan;

	berserkerArmyCount = knightArmyCount = priestArmyCount = rangerArmyCount = 
		berserkerSquadCount = knightSquadCount = priestSquadCount = rangerSquadCount = 0;

	for(unsigned int i = 0; i < playerOne->Army.size(); i++)
	{

		tempArmyVec.push_back(playerOne->Army[i]);
		tempSquadAddVec.push_back(playerOne->Army[i]);		
	}

	AddToSquad(playerOne, playerOne->Army[0]);
	xRight = xLeft = false;
}
/*virtual*/ void SelectSquadState::Exit()
{

}
/*virtual*/ bool SelectSquadState::Input()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();
	//if start is pressed, call AddToSquad
	if(DI->JoystickIsUnplugged())
	{
		if(DI->KeyPressed( DIK_LEFT) == true)
		{
			--cursor;

			if(cursor < 0)
				cursor = 0;
		}
		else if(DI->KeyPressed(DIK_RIGHT) == true)
		{
			++cursor;

			if(cursor > 3)
				cursor = 3;
		}

		if(DI->KeyPressed(DIK_RETURN) == true)
		{
			switch(cursor)
			{
			case 0:
				{
					if(playerOne->Squad.size() >= 9)
					{
						break;
					}
					else if(playerOne->Squad.size() < 9)
					{
						for(unsigned int i = 0; i < playerOne->Army.size(); i++)
						{
							if(playerOne->Army[i]->GetType() == 1)
							{
								AddToSquad(playerOne, playerOne->Army[i]);
								playerOne->Army.erase(playerOne->Army.begin() + i);
								if(berserkerSquadCount < 2)
								{
									berserkerSquadCount++;
								}
							}
						}
					}
				}
				break;
			case 1:
				{
					if(playerOne->Squad.size() >= 9)
					{
						break;
					}
					else if(playerOne->Squad.size() < 9)
					{
						for(unsigned int i = 0; i < playerOne->Army.size(); i++)
						{
							if(playerOne->Army[i]->GetType() == 2)
							{
								AddToSquad(playerOne, playerOne->Army[i]);
								playerOne->Army.erase(playerOne->Army.begin() + i);
								if(knightSquadCount < 2)
								{
									knightSquadCount++;
								}
							}
						}
					}
				}
				break;
			case 2:
				{
					if(playerOne->Squad.size() >= 9)
					{
						break;
					}
					else if(playerOne->Squad.size() < 9)
					{

						for(unsigned int i = 0; i < playerOne->Army.size(); i++)
						{
							if(playerOne->Army[i]->GetType() == 3)
							{
								AddToSquad(playerOne, playerOne->Army[i]);
								playerOne->Army.erase(playerOne->Army.begin() + i);
								if(priestSquadCount < 2)
								{
									priestSquadCount++;
								}
							}
						}
					}
				}
				break;
			case 3:
				{
					if(playerOne->Squad.size() >= 9)
					{
						break;
					}
					else if(playerOne->Squad.size() < 9)
					{
						for(unsigned int i = 0; i < playerOne->Army.size(); i++)
						{
							if(playerOne->Army[i]->GetType() == 4)
							{
								AddToSquad(playerOne, playerOne->Army[i]);
								playerOne->Army.erase(playerOne->Army.begin() + i);
								if(rangerSquadCount < 2)
								{
									rangerSquadCount++;
								}
							}
						}
					}
				}
				break;
			}
		}
		else if(DI->KeyPressed(DIK_BACKSPACE) == true)
		{
			switch(cursor)
			{
			case 0:
				{
					if(playerOne->Squad.size() < 0)
					{
						break;
					}
					else if(playerOne->Squad.size() > 0)
					{
						for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
						{
							if(playerOne->Squad[i]->GetType() == 1)
							{
								playerOne->Army.push_back(playerOne->Squad[i]);
								playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);
								if(berserkerSquadCount > 0)
								{
									berserkerSquadCount--;
								}
							}
						}
					}
				}
				break;
			case 1:
				{
					if(playerOne->Squad.size() < 0)
					{
						break;
					}
					else if(playerOne->Squad.size() > 0)
					{
						for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
						{
							if(playerOne->Squad[i]->GetType() == 2)
							{
								playerOne->Army.push_back(playerOne->Squad[i]);
								playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);
								if(knightSquadCount > 0)
								{
									knightSquadCount--;
								}
								//i++;
							}
						}
					}
				}
				break;
			case 2:
				{
					if(playerOne->Squad.size() < 0)
					{
						break;
					}
					else if(playerOne->Squad.size() > 0)
					{
						for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
						{
							if(playerOne->Squad[i]->GetType() == 3)
							{
								playerOne->Army.push_back(playerOne->Squad[i]);
								playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);
								if(priestSquadCount > 0)
								{
									priestSquadCount--;
								}
								i++;
							}
						}
					}
				}
				break;
			case 3:
				{
					if(playerOne->Squad.size() < 0)
					{
						break;
					}
					else if(playerOne->Squad.size() > 0)
					{
						for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
						{
							if(playerOne->Squad[i]->GetType() == 4)
							{
								playerOne->Army.push_back(playerOne->Squad[i]);
								playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);	
								if(rangerSquadCount > 0)
								{
									rangerSquadCount--;
								}
								i++;
							}
						}
					}
				}
				break;
			}
		}

		if(DI->KeyPressed(DIK_ESCAPE) == true)
		{
			Game::GetInstance()->PopState();
		}
		if(DI->KeyPressed(DIK_SPACE) == true)
		{
			Game::GetInstance()->PushState(GameplayState::GetInstance());
		}
	}
	else
	{
		Keyboard();
	}
	return true;
}
/*virtual*/ void SelectSquadState::Update( float fElapsedTime )
{
	//playerOne->UpdateAll(fElapsedTime, playerOne->Squad);
	tempArmyVec.clear();
	for(unsigned int i = 0; i < playerOne->Army.size(); i++)
	{
		//tempArmyVec is full and untouched
		tempArmyVec.push_back(playerOne->Army[i]);
		berserkerArmyCount = knightArmyCount = priestArmyCount = rangerArmyCount = 0;
	}

	for(unsigned int i = 0; i < tempArmyVec.size(); i++)
	{
		if(tempArmyVec[i]->GetType() == 1)
		{
			berserkerArmyCount++;
			tempArmyVec.erase(tempArmyVec.begin() + i);
			i--;
		}
		else if(tempArmyVec[i]->GetType() == 2)
		{
			knightArmyCount++;
			tempArmyVec.erase(tempArmyVec.begin() + i);
			i--;
		}
		else if(tempArmyVec[i]->GetType() == 3)
		{
			priestArmyCount++;
			tempArmyVec.erase(tempArmyVec.begin() + i);
			i--;
		}
		else if(tempArmyVec[i]->GetType() == 4)
		{
			rangerArmyCount++;
			tempArmyVec.erase(tempArmyVec.begin() + i);
			i--;
		}
	}
	//tempArmyVec is empty

	//tempSquadVec.clear();
	for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
	{
		tempSquadVec.push_back(playerOne->Squad[i]);
		berserkerSquadCount = knightSquadCount = priestSquadCount = rangerSquadCount = 0;
	}

	for(unsigned int i = 0; i < tempSquadVec.size(); i++)
	{
		if(tempSquadVec[i]->GetType() == 1)
		{
			berserkerSquadCount++;
			tempSquadVec.erase(tempSquadVec.begin() + i);
			i--;
		}
		else if(tempSquadVec[i]->GetType() == 2)
		{
			knightSquadCount++;
			tempSquadVec.erase(tempSquadVec.begin() + i);
			i--;
		}
		else if(tempSquadVec[i]->GetType() == 3)
		{
			priestSquadCount++;
			tempSquadVec.erase(tempSquadVec.begin() + i);
			i--;
		}
		else if(tempSquadVec[i]->GetType() == 4)
		{
			rangerSquadCount++;
			tempSquadVec.erase(tempSquadVec.begin() + i);
			i--;
		}
	}

}
/*virtual*/ void SelectSquadState::Render()
{
	BitmapFont* Font = Game::GetInstance()->GetFont();
	CSGD_TextureManager::GetInstance()->Draw(titleImgID, 0, 0, 1.0f, 1.2f);

	Font->Draw( _T("SELECT SQUAD"), (int)((340 - (3*24*2.0f)) / 2), 
		50, 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetBerserkerImgID(), 200, 200, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetKnightImgID(), 300, 200, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetPriestImgID(), 400, 200, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetRangerImgID(), 500, 200, 1.0f, 1.0f);

	TOSTRINGSTREAM unitCount;

	unitCount << berserkerArmyCount;
	Font->Draw(unitCount.str().c_str(), 232, 232, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	unitCount << knightArmyCount;
	Font->Draw(unitCount.str().c_str(), 332, 232, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	unitCount << priestArmyCount;
	Font->Draw(unitCount.str().c_str(), 432, 232, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	unitCount << rangerArmyCount;
	Font->Draw(unitCount.str().c_str(), 532, 232, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetGeneralImgID(), 100, 400, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetBerserkerImgID(), 200, 400, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetKnightImgID(), 300, 400, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetPriestImgID(), 400, 400, 1.0f, 1.0f);
	CSGD_TextureManager::GetInstance()->Draw(Game::GetInstance()->GetRangerImgID(), 500, 400, 1.0f, 1.0f);

	unitCount << berserkerSquadCount;
	Font->Draw(unitCount.str().c_str(), 232, 432, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	unitCount << knightSquadCount;
	Font->Draw(unitCount.str().c_str(), 332, 432, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	unitCount << priestSquadCount;
	Font->Draw(unitCount.str().c_str(), 432, 432, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	unitCount << rangerSquadCount;
	Font->Draw(unitCount.str().c_str(), 532, 432, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));
	unitCount.str(_T(""));

	Font->Draw( _T("(   )"), Game::GetInstance()->GetScreenWidth()/5 + (110 * cursor), 210, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

}
void SelectSquadState::AddToSquad(Player* player, Entity* Unit) const
{
	player->AddUnit(Unit, player->Squad);
}
bool SelectSquadState::Keyboard()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();
	if(DI->JoystickGetLStickXNormalized() < 0 && xLeft == false)
	{
		--cursor;

		if(cursor < 0)
			cursor = 0;
		xLeft = true;
	}
	else if(DI->JoystickGetLStickXNormalized() > 0 && xRight == false)
	{
		++cursor;

		if(cursor > 3)
			cursor = 3;
		xRight = true;
	}

	if(DI->JoystickButtonPressed(0) == true)
	{
		switch(cursor)
		{
		case 0:
			{
				if(playerOne->Squad.size() >= 8)
				{
					break;
				}
				else if(playerOne->Squad.size() < 8)
				{
					for(unsigned int i = 0; i < /*tempSquadAddVec*/playerOne->Army.size(); i++)
					{
						if(/*tempSquadAddVec*/playerOne->Army[i]->GetType() == 1)
						{
							//adds to playerOne->Squad and removes from tempsquadaddvec(army)
							AddToSquad(playerOne, /*tempSquadAddVec*/playerOne->Army[i]);
							//tempSquadAddVec.erase(tempSquadAddVec.begin() + i);
							playerOne->Army.erase(playerOne->Army.begin() + i);
							if(berserkerSquadCount < 2)
							{
								berserkerSquadCount++;
							}
							//i++;
						}
					}
				}
			}
			break;
		case 1:
			{
				if(playerOne->Squad.size() >= 8)
				{
					break;
				}
				else if(playerOne->Squad.size() < 8)
				{
					for(unsigned int i = 0; i < /*tempSquadAddVec*/playerOne->Army.size(); i++)
					{
						if(/*tempSquadAddVec*/playerOne->Army[i]->GetType() == 2)
						{
							//adds to playerOne->Squad and removes from tempsquadaddvec(army)
							AddToSquad(playerOne, /*tempSquadAddVec*/playerOne->Army[i]);
							//tempSquadAddVec.erase(tempSquadAddVec.begin() + i);
							playerOne->Army.erase(playerOne->Army.begin() + i);
							if(knightSquadCount < 2)
							{
								knightSquadCount++;
							}
							//i++;
						}
					}
				}
			}
			break;
		case 2:
			{
				if(playerOne->Squad.size() >= 8)
				{
					break;
				}
				else if(playerOne->Squad.size() < 8)
				{

					for(unsigned int i = 0; i < /*tempSquadAddVec*/playerOne->Army.size(); i++)
					{
						if(/*tempSquadAddVec*/playerOne->Army[i]->GetType() == 3)
						{
							//adds to playerOne->Squad and removes from tempsquadaddvec(army)
							AddToSquad(playerOne, /*tempSquadAddVec*/playerOne->Army[i]);
							//tempSquadAddVec.erase(tempSquadAddVec.begin() + i);
							playerOne->Army.erase(playerOne->Army.begin() + i);
							if(priestSquadCount < 2)
							{
								priestSquadCount++;
							}
							//i++;
						}
					}
				}
			}
			break;
		case 3:
			{
				if(playerOne->Squad.size() >= 8)
				{
					break;
				}
				else if(playerOne->Squad.size() < 8)
				{
					for(unsigned int i = 0; i < /*tempSquadAddVec*/playerOne->Army.size(); i++)
					{
						if(/*tempSquadAddVec*/playerOne->Army[i]->GetType() == 4)
						{
							//adds to playerOne->Squad and removes from tempsquadaddvec(army)
							AddToSquad(playerOne, /*tempSquadAddVec*/playerOne->Army[i]);
							//tempSquadAddVec.erase(tempSquadAddVec.begin() + i);
							playerOne->Army.erase(playerOne->Army.begin() + i);
							if(rangerSquadCount < 2)
							{
								rangerSquadCount++;
							}
							//i++;
						}
					}
				}
			}
			break;
		}
	}
	else if(DI->JoystickButtonPressed(2) == true)
	{
		switch(cursor)
		{
		case 0:
			{
				if(playerOne->Squad.size() < 0)
				{
					break;
				}
				else if(playerOne->Squad.size() > 0)
				{
					for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
					{
						if(playerOne->Squad[i]->GetType() == 1)
						{
							//adds unit from squad back into army. removes from playerOne->Squad
							/*tempSquadAddVec*/playerOne->Army.push_back(playerOne->Squad[i]);
							playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);
							if(berserkerSquadCount > 0)
							{
								berserkerSquadCount--;
							}
							//i++;
						}
					}
				}
			}
			break;
		case 1:
			{
				if(playerOne->Squad.size() < 0)
				{
					break;
				}
				else if(playerOne->Squad.size() > 0)
				{
					for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
					{
						if(playerOne->Squad[i]->GetType() == 2)
						{
							playerOne->Army.push_back(playerOne->Squad[i]);
							playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);
							if(knightSquadCount > 0)
							{
								knightSquadCount--;
							}
							//i++;
						}
					}
				}
			}
			break;
		case 2:
			{
				if(playerOne->Squad.size() < 0)
				{
					break;
				}
				else if(playerOne->Squad.size() > 0)
				{
					for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
					{
						if(playerOne->Squad[i]->GetType() == 3)
						{
							playerOne->Army.push_back(playerOne->Squad[i]);
							playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);
							if(priestSquadCount > 0)
							{
								priestSquadCount--;
							}
							i++;
						}
					}
				}
			}
			break;
		case 3:
			{
				if(playerOne->Squad.size() < 0)
				{
					break;
				}
				else if(playerOne->Squad.size() > 0)
				{
					for(unsigned int i = 0; i < playerOne->Squad.size(); i++)
					{
						if(playerOne->Squad[i]->GetType() == 4)
						{
							playerOne->Army.push_back(playerOne->Squad[i]);
							playerOne->RemoveUnit(playerOne->Squad[i], playerOne->Squad);	
							if(rangerSquadCount > 0)
							{
								rangerSquadCount--;
							}
							i++;
						}
					}
				}
			}
			break;
		}
	}

	if(DI->JoystickButtonPressed(1) == true)
	{
		Game::GetInstance()->PopState();
	}
	if(DI->JoystickButtonPressed(7) == true)
	{
		Game::GetInstance()->PushState(GameplayState::GetInstance());
	}
	if(DI->JoystickGetLStickXNormalized() == 0)
	{
		xRight = false;
		xLeft = false;
	}
	return true;
}